package com.jelly.web.share.upload.service.impl;

import com.jelly.common.constant.Constant;
import com.jelly.common.enums.WebsiteDetailsType;
import com.jelly.common.exception.UploadImgException;
import com.jelly.common.util.upload.OSSClientUtil;
import com.jelly.common.util.upload.UploadUtil;
import com.jelly.web.modules.basics.websiteDetails.domain.WebUpload;
import com.jelly.web.modules.basics.websiteDetails.domain.WebsiteDetails;
import com.jelly.web.modules.basics.websiteDetails.service.IWebsiteDetailsService;
import com.jelly.web.share.upload.service.IUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @创建人 ql
 * @创建时间 2018-10-18 14:52
 * @项目名称 jelly
 * @功能描述: 上传实现类
 **/
@Service
public class UploadServiceImpl implements IUploadService {

    @Autowired
    private IWebsiteDetailsService websiteDetailsService;

    @Override
    public String upload(MultipartFile file, String type) throws IOException {

        //获取网站上传信息
        WebsiteDetails websiteDetails = websiteDetailsService.getWebsiteDetails(WebsiteDetailsType.WEBUPLOAD.getValue());

        //不为空并且上传状态为正常
        if(null != websiteDetails && Constant.SUCCESS.equals(websiteDetails.getIsState())){

            WebUpload webUpload = websiteDetails.getWebUpload();
            //判断OSS上传是否正常   如正常默认使用OSS上传
            if(Constant.SUCCESS.equals(webUpload.getIsOSSState())){

                //拼接访问地址
                return webUpload.getAccessUrl() + "/" + OSSClientUtil.uploadImg(file,webUpload,type);

            }else{

                return "/"+UploadUtil.uploadFile(file,webUpload.getLocalPath(),type);

            }

        }

        throw new UploadImgException("后台已禁止上传");

    }

}
